package com.hopes.bizz.risk.control.vo;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import com.alibaba.excel.enums.BooleanEnum;
import com.alibaba.excel.enums.poi.HorizontalAlignmentEnum;
import com.alibaba.excel.enums.poi.VerticalAlignmentEnum;
import com.hopes.bizz.common.core.annotation.ExcelDictValidate;
import com.hopes.bizz.common.core.annotation.ExcelNotNull;
import com.hopes.bizz.common.core.validate.group.AddGroup;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskEntity;
import com.hopes.bizz.risk.control.entity.RiskUnitEventEntity;
import com.hopes.boss.common.entity.ExcelDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @author yumr
 * @ProjectName bizz
 * @Description 风险辨识管控清单导出
 * @time 2023/7/24 11:01
 */
@Data
@Builder
@ColumnWidth(20)
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(30)
@ContentRowHeight(20)
@HeadStyle( horizontalAlignment = HorizontalAlignmentEnum.CENTER, verticalAlignment = VerticalAlignmentEnum.CENTER)
@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.LEFT, verticalAlignment = VerticalAlignmentEnum.CENTER, wrapped = BooleanEnum.TRUE)
public class RiskDetailExcelVO extends ExcelDTO implements Serializable {

    private static final long serialVersionUID = 2456563733769178483L;

    @ExcelIgnore
    private Long id;

    @ExcelProperty("序号")
    private String index;

    @ExcelIgnore
    private Long riskObjectId;

    @ExcelProperty({"风险分析对象", "对象名称"})
    @ExcelNotNull(message = "风险分析对象名称不可为空")
    private String objectHazardName;

    @ExcelProperty({"风险分析对象", "对象编码"})
    @ExcelNotNull(message = "风险分析对象编码不可为空")
    private String objectHazardCode;

    @ExcelProperty({"风险分析对象", "对象类型"})
    @ExcelNotNull(message = "风险分析对象类型不可为空")
    @ExcelDictValidate(enumResolver = "risk_object_type")
    private String objectHazardTypeStr;

    @ExcelProperty(value = {"风险分析对象", "责任部门"}, index = 4)
    @ExcelDictValidate(enumResolver = "sys_dept", multiple = true)
    private String objectDeptIdStr;

    @ExcelProperty(value = {"风险分析对象", "责任人"}, index = 5)
    @ExcelDictValidate(enumResolver = "sys_user", multiple = true)
    private String objectPrincipalStr;

    @ExcelProperty({"风险分析对象", "重大危险源"})
    @ExcelDictValidate(enumResolver = "yes_no_type")
    private String hazardSourceFlagStr;


    @ExcelIgnore
    private Long riskUnitId;

    @ExcelProperty({"风险分析单元", "单元类型"})
    @ExcelDictValidate(enumResolver = "risk_unit_type")
    private String unitTypeStr;

    @ExcelNotNull(message = "风险分析单元名称必填")
    @ExcelProperty({"风险分析单元", "单元名称"})
    private String unitName;

    @ExcelNotNull(message = "风险分析单元责任部门必填")
    @ExcelProperty(value = {"风险分析单元", "责任部门"}, index = 9)
    @ExcelDictValidate(enumResolver = "sys_dept", multiple = true)
    private String unitDeptIdStr;

    @ExcelNotNull(message = "风险分析单元责任人必填")
    @ExcelProperty(value = {"风险分析单元", "责任人"}, index = 10)
    @ExcelDictValidate(enumResolver = "sys_user", multiple = true)
    private String unitPrincipalStr;

    @ExcelNotNull(message = "风险分析单元评估方法必填")
    @ExcelProperty({"风险分析单元", "评估方法"})
    @ExcelDictValidate(enumResolver = "risk_evaluate_method")
    private String evaluateMethodStr;

    /* ----------- 风险事件 --------------- */
    @ExcelIgnore
    private Long unitEventId;

    @ExcelProperty({"风险事件", "风险事件"})
    @ExcelNotNull(message = "风险事件不可为空")
    private String eventName;

    @ExcelProperty({"风险事件", "作业步骤"})
    @ExcelNotNull(message = "作业步骤不可为空")
    private String jobStep;

    @ExcelProperty({"风险事件", "主要后果"})
    private String majorConsequenceDesc;

    @ExcelProperty({"风险事件", "固有风险等级"})
    @ExcelDictValidate(enumResolver = "risk_level")
    private String riskLevelStr;

    @ExcelProperty({"风险事件", "固有风险描述"})
    private String riskDesc;

    @ExcelProperty({"风险事件", "现状风险等级"})
    @ExcelDictValidate(enumResolver = "risk_level")
    private String afterRiskLevelStr;

    @ExcelProperty({"风险事件", "现状风险描述"})
    private String afterRiskDesc;

    /* --------------- 管控措施 ------------  */
    @ExcelIgnore
    private Long controlMeasureId;

    @ExcelNotNull(message = "管控措施中管控层级必填")
    @ExcelProperty({"管控措施", "管控层级"})
    @ExcelDictValidate(enumResolver = "risk_control_level")
    private String controlLevelStr;

    @ExcelNotNull(message = "管控措施中措施描述必填")
    @ExcelProperty({"管控措施", "管控措施描述"})
    private String riskMeasureDesc;

    @ExcelNotNull(message = "管控措施中排查内容必填")
    @ExcelProperty({"管控措施", "管控排查内容"})
    private String checkContent;

    @ExcelNotNull(message = "管控措施中措施分类1必填")
    @ExcelProperty({"管控措施", "措施分类1"})
    @ExcelDictValidate(enumResolver = "risk_control_classify")
    private String classify1Str;

    @ExcelNotNull(message = "管控措施中措施分类2必填")
    @ExcelProperty({"管控措施", "措施分类2"})
    @ExcelDictValidate(enumResolver = "risk_control_classify")
    private String classify2Str;

    @ExcelProperty({"管控措施", "措施分类3"})
    private String classify3;

    @ExcelProperty({"管控措施", "相关法规标准"})
    private String lawDesc;

    /* ----------- 隐患排查任务设置 ------------- */
    @ExcelIgnore
    private Long taskCheckContentId;  // 隐患排查任务Id

    @ExcelProperty({"管控任务", "隐患排查内容"})
    private String taskCheckContent;

    @ExcelIgnore
    private Long riskControlTaskId; // 任务Id

    @ExcelProperty({"管控任务", "任务分级"})
    @ExcelDictValidate(enumResolver = "risk_control_level")
    private String taskControlLevelStr;

    @ExcelProperty(value = {"管控任务", "责任人"}, index = 28)
    @ExcelDictValidate(enumResolver = "sys_user", multiple = true)
    private String responsibleUserIdsStr;

    @ExcelProperty({"管控任务", "周期"})
    private String checkCycle;

    @ExcelProperty({"管控任务", "单位"})
    @ExcelDictValidate(enumResolver = "check_cycle_type")
    private String checkCycleTypeStr;

    @ExcelProperty({"管控任务", "任务类型"})
    private String workTypeStr;

    @ExcelProperty({"管控任务", "包保任务对应项"})
    private String taskNumStr;

    @ExcelIgnore
    private String attachmentFile;
    @ExcelIgnore
    private int rowCount;

    public RiskDetailExcelVO(RiskControlMeasureVO controlMeasureVO, RiskUnitEventVO riskUnitEventVO,
                             RiskMeasureTaskConfigVO taskConfig, RiskMeasureTaskEntity measureTask) {
        if (null != controlMeasureVO) {
            buildControlMeasure(controlMeasureVO);
            if (null != controlMeasureVO.getRiskObject()) {
                final RiskObjectVO riskObject = controlMeasureVO.getRiskObject();
                buildRiskObject(riskObject);
            }
        }
        // 获取风险事件
        if (null != riskUnitEventVO) {
            buildRiskUnitEvent(riskUnitEventVO);
            if (null != riskUnitEventVO.getRiskUnit()) {
                buildRiskUnit(riskUnitEventVO);
            }
        }
        // 管控任务
        if (null != taskConfig) {
            this.taskCheckContentId = taskConfig.getId();
            this.taskCheckContent = taskConfig.getCheckContent();
        }
        if (null != measureTask) {
            this.riskControlTaskId = measureTask.getId();
            this.taskControlLevelStr = measureTask.getControlLevelStr();
            this.responsibleUserIdsStr = measureTask.getResponsibleUserIdsStr();
            this.checkCycle = String.valueOf(measureTask.getCheckCycle());
            this.checkCycleTypeStr = measureTask.getCheckCycleTypeStr();
            this.workTypeStr = measureTask.getWorkTypeStr();
            this.taskNumStr = measureTask.getTaskNumStr();
        }
    }

    private void buildRiskUnit(RiskUnitEventVO riskUnitEventVO) {
        final RiskUnitVO riskUnitVO = riskUnitEventVO.getRiskUnit();
        this.riskUnitId = riskUnitVO.getId();
        this.unitName = riskUnitVO.getRiskName();
        this.unitDeptIdStr = riskUnitVO.getDeptIdStr();
        this.unitTypeStr = riskUnitVO.getRiskUnitTypeStr();
        this.unitPrincipalStr = riskUnitVO.getPrincipalStr();
        this.evaluateMethodStr = riskUnitVO.getEvaluateMethodStr();
    }

    private void buildRiskUnitEvent(RiskUnitEventVO riskUnitEventVO) {
        this.unitEventId = riskUnitEventVO.getId();
        this.jobStep = riskUnitEventVO.getJobStep();
        this.eventName = riskUnitEventVO.getEventName();
        this.majorConsequenceDesc = riskUnitEventVO.getMajorConsequenceDesc();
        this.riskLevelStr = riskUnitEventVO.getRiskLevelStr();
        this.riskDesc = riskUnitEventVO.getRiskDesc();
        this.afterRiskLevelStr = riskUnitEventVO.getAfterRiskLevelStr();
        this.afterRiskDesc = riskUnitEventVO.getAfterRiskDesc();
    }

    private void buildRiskObject(RiskObjectVO riskObject) {
        this.riskObjectId = riskObject.getId();
        this.objectHazardName = riskObject.getHazardName();
        this.objectHazardCode = riskObject.getHazardCode();
        this.objectHazardTypeStr = riskObject.getHazardTypeStr();
        this.objectDeptIdStr = riskObject.getDeptIdStr();
        this.objectPrincipalStr = riskObject.getPrincipalStr();
        this.hazardSourceFlagStr = riskObject.getHazardSourceFlagStr();
    }

    private void buildControlMeasure(RiskControlMeasureVO controlMeasureVO) {
        this.controlMeasureId = controlMeasureVO.getId();
        this.controlLevelStr = controlMeasureVO.getControlLevelStr();
        this.riskMeasureDesc = controlMeasureVO.getRiskMeasureDesc();
        this.checkContent = controlMeasureVO.getCheckContent();
        this.classify1Str = controlMeasureVO.getClassify1Str();
        this.classify2Str = controlMeasureVO.getClassify2Str();
        this.classify3 = controlMeasureVO.getClassify3();
        this.lawDesc = controlMeasureVO.getLawDesc();
    }


}
